package barbershop.com.dao.orders;

import barbershop.com.entity.orders.Orders;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface OrderDao extends JpaRepository<Orders, Integer> {
    // 更新总价与总数量
    @Modifying
    @Query(value = "update orders set order_total_quantity=?1, order_total_price=?2 where order_id=?3",
            nativeQuery = true)
    void updateTotalQP(Integer totalQuantity, Double totalPrice, Integer orderId);

    // 修改订单状态
    @Modifying
    @Query(value = "update orders set order_status=?1 where order_id=?2",
            nativeQuery = true)
    void updateOrderStatus(Integer status, Integer orderId);

    // 获取交易成功的订单
    List<Orders> findAllByOrderStatus(Integer orderStatus);

    // 按照日期查询订单
    @Query(value = "select * from orders where date_format(order_date,'%Y-%m-%d')=?1",
            nativeQuery = true)
    List<Orders> findOrdersByRecentDay(String date);

    // 按照过去七天的周期查询订单
    @Query(value = "select * from orders where date_format(order_date,'%Y-%m-%d')>=?1 " +
            "and date_format(order_date,'%Y-%m-%d')<=?2 and order_status=?3",
            nativeQuery = true)
    List<Orders> findOrdersByRecentWeek(String startDate, String endDate, Integer orderStatus);
}
